package com.atguigu.gulimall.seckill.scheduled;

import com.atguigu.gulimall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;


/**
 * 秒杀商品定时上架
 *  每天晚上3点，上架最近三天需要三天秒杀的商品
 *  当天00:00:00 - 23:59:59
 *  明天00:00:00 - 23:59:59
 *  后天00:00:00 - 23:59:59
 */

@Slf4j
@Service
public class SeckillScheduled {

    @Autowired
    private SeckillService seckillService;

    @Autowired
    private RedissonClient redissonClient;

    //秒杀商品上架功能的锁
    private final String upload_lock = "seckill:upload:lock";

    //TODO 保证幂等性问题
     @Scheduled(cron = "*/30 * * * * ? ")
//    @Scheduled(cron = "0 0 3 * * ? ")
    public void uploadSeckillSkuLatest3Days() {
        //1、重复上架无需处理
        log.info("上架秒杀的商品...");

        //分布式锁
        RLock lock = redissonClient.getLock(upload_lock);
        try {
            //加锁
            lock.lock(10, TimeUnit.SECONDS);
            seckillService.uploadSeckillSkuLatest3Days();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    //TODO 保证心跳
    @Scheduled(cron = "0 */5 * * * ? ")
//    @Scheduled(cron = "0 0 3 * * ? ")
    public void xinTiao() {
         log.info("保存服务不中断");
        RestTemplate restTemplate = new RestTemplate();
        //auth服务
        restTemplate.getForObject("http://192.168.1.101:31000/login.html",String.class);
        //cart服务
        restTemplate.getForObject("http://192.168.1.101:31001/currentUserCartItems",String.class);
        //coupon服务
        restTemplate.getForObject("http://192.168.1.101:31002/coupon/coupon/list",String.class);
        //geteway服务
        restTemplate.getForObject("http://junjie2.natapp1.cc/product/skuinfo/5/price",String.class);
        //member服务
        restTemplate.getForObject("http://192.168.1.101:31004/member/member/list",String.class);
        //order服务
        restTemplate.getForObject("http://192.168.1.101:31005/order/order/status/sfa",String.class);
        //product服务
        restTemplate.getForObject("http://192.168.1.101:31006/product/skuinfo/5/price",String.class);
        //search服务
        restTemplate.getForObject("http://192.168.1.101:31007/list.html",String.class);
        //seckill服务无需
        //
        //third服务
        restTemplate.getForObject("http://192.168.1.101:31009/test",String.class);
        //ware服务
        restTemplate.getForObject("http://192.168.1.101:31010/ware/waresku/list",String.class);
        //renren服务
        restTemplate.getForObject("http://192.168.1.101:32207/#/login",String.class);
        log.info("成功保存服务不中断");
    }

    public static void main(String[] args) {
        SeckillScheduled seckillScheduled = new SeckillScheduled();
        seckillScheduled.xinTiao();
    }
}
